#!/bin/bash 
set -e


TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage4}
SRC=${SRC:-/sources}

name=p11-kit
version=0.23.22

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-$version.tar.xz -C $TMP

{ time \
   {

    cd $TMP/$name-$version
    
	sed '20,$ d' -i trust/trust-extract-compat
	cat >> trust/trust-extract-compat << "EOF"
# Copy existing anchor modifications to /etc/ssl/local
/usr/libexec/make-ca/copy-trust-modifications

# Generate a new trust store
/usr/sbin/make-ca -f -g
EOF

	./configure --prefix=/usr     \
	            --sysconfdir=/etc \
	            --with-trust-paths=/etc/pki/anchors \
	            --with-hash-impl=freebl   \
	            --enable-doc
	make

	make install
	ln -sfv /usr/libexec/p11-kit/trust-extract-compat \
	        /usr/bin/update-ca-certificates

	ln -sfv ./pkcs11/p11-kit-trust.so /usr/lib/libnssckbi.so

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
